package top.zhangjianyong.tools.wechat;

import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.zhangjianyong.tools.generation.GenerationCaller;
import top.zhangjianyong.tools.generation.GenerationMessage;
import top.zhangjianyong.tools.parm.wechat.WechatRequestMessage;
import top.zhangjianyong.tools.parm.wechat.WechatResponseMessage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangjianyong
 * @since 2025/3/20 下午4:42
 */
@Slf4j
@Component
public class WechatChat {

    @Autowired
    private GenerationCaller generationCaller;


    public WechatResponseMessage call(WechatRequestMessage message) {
        String fromUserName = message.getFromUserName();
        WechatContent wechatContent = WechatContentHolder.get(fromUserName);
        List<Message> messages = wechatContent.getMessages();
        messages.add(GenerationMessage.createUserMessage(message.getContent()));
        try {
            GenerationResult generationResult = generationCaller.callWithMessage(messages);
            Message respMessage = generationResult.getOutput().getChoices().get(0).getMessage();
            messages.add(respMessage);
            String content = respMessage.getContent();
            return message.response(content);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return message.response("我有点懵，请稍后再问我吧");
    }
}
